<?php

namespace app\admin\model\mental;

use PHPQRCode\QRcode;
use think\Model;


class Order extends Model
{



    // 表名
    protected $name = 'mental_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'paytime_text',
        'status_text',
        'type_text',
        'paytype_text',
        'url',
        'qrcode'
    ];

    public function getUrlAttr($value, $data)
    {
        // $ret=config('site.qdDomain') .'/pages/report/guide?test_id='.$data['test_id'].'&main_id='.$data['main_id'];
        $ret = "";
        if ($data['type'] == 6) {
            $ret = "";
        } else {
            $isTemplate = array_key_exists('template', $data);
            if ($isTemplate) {
                $template = $data['template'];
            } else {
                $template = \think\Db::name('mental_testmain')->where('id', $data['main_id'])->value('template');
            }

            if ($template == 0 || $template == 1) {
                $ret = config('site.qdDomain') . "/pages/report/report?test_id=" . $data['test_id'] . "&main_id=" . $data['main_id'];
            } elseif ($template == 2) {
                $ret = config('site.qdDomain') . "/pages/report/dg_rep?test_id=" . $data['test_id'] . "&main_id=" . $data['main_id'];
            } elseif ($template == 3) {
                $ret = config('site.qdDomain') . "/pages/report/zzwd_rep?test_id=" . $data['test_id'] . "&main_id=" . $data['main_id'];
            } elseif ($template == 4) {
                $ret = "模板还没写"; //模板还没写
            }
        }

        // var_dump($data);die;
        return $ret;
    }


    public function getQrcodeAttr($value, $data)
    {
//        require_once THINK_PATH . 'phpqrcode.php';
        $url = config('site.qdDomain') ."/pages/report/report?test_id=" . $data['test_id'] . "&main_id=" . $data['main_id'];
        ob_start();
        //生成二维码图片
        $returnData = QRcode::png($url, false, "H", 3, 1);
        //这里就是把生成的图片流从缓冲区保存到内存对象上，使用base64_encode变成编码字符串，通过json返回给页面。
        $imageString = base64_encode(ob_get_contents());
        //关闭缓冲区
        ob_end_clean();
        $str = "data:image/png;base64," . $imageString;
        return $str;
    }

    public function getPaytypeTextAttr($value, $data)
    {
        $text = '';
        if ($data['paytype'] == 'wechat') {
            $text = '微信';
        } elseif ($data['paytype'] == 'alipay') {
            $text = '支付宝';
        } elseif ($data['paytype'] == 'code') {
            $text = '兑换码';
        }
        return $text;
    }

    public function getStatusList()
    {
//        return ['created' => __('Created'), 'paid' => __('Paid'), 'expired' => __('Expired')];
        return ['created' => __('Created'), 'paid' => __('Paid')];
    }

    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function TestMain()
    {
        return $this->hasOne('TestMain', 'id', 'main_id')->bind('title,template');
    }


    public function extension()
    {
        // return $this->belongsTo('Extension', 'extensionId', 'id', [], 'LEFT')->setEagerlyType(0);
        return $this->hasOne('Extension', 'id', 'extensionId')->bind('channel_type_text');
    }

    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
